<template>
	<el-container>
		<el-aside width="200px" v-loading="menuloading">
			<el-container>
				<el-header>
					<el-input placeholder="输入关键字进行过滤" v-model="menuFilterText" clearable></el-input>
				</el-header>
				<el-main class="nopadding">
					<el-tree ref="menu" class="menu" node-key="id" :data="menuList" :props="menuProps" :current-node-key="''"
							 draggable highlight-current :expand-on-click-node="false"
							 :filter-node-method="menuFilterNode" @node-click="menuClick"></el-tree>
				</el-main>
				<el-footer style="height:51px;">
					<el-button type="primary" size="small" icon="el-icon-plus" v-auth="'cate.add'" @click="addType"></el-button>
					<el-button type="success" size="small" plain icon="el-icon-edit" v-auth="'cate.edit'" @click="editMenu"></el-button>
					<el-button type="danger" size="small" plain icon="el-icon-delete" v-auth="'cate.delete'" @click="delMenu"></el-button>
				</el-footer>
			</el-container>
		</el-aside>
		<el-container>
			<el-header>
				<div class="right-panel">
					<div class="right-panel-search">
						<el-input v-model="search.keywords" placeholder="物料编号/名称/规格型号" clearable @change="handleKeyDown"></el-input>
<!--						<el-select style="margin:5px 10px" v-model="search.unit_id" filterable clearable placeholder="请选择单位">-->
<!--							<el-option v-for="item in unitList" :key="item.id" :label="item.unit_name" :value="item.id"/>-->
<!--						</el-select>-->
						<el-select v-model="search.pay_type" filterable placeholder="请选择购买方式">
							<el-option label="自制" :value="1" />
							<el-option label="外购" :value="2" />
						</el-select>
						<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
						<el-button type="info" @click="refresh">重置</el-button>
					</div>
				</div>
				<div class="left-panel">
					<el-button type="primary" @click="add" v-auth="'spare.add'">新增</el-button>
					<el-button type="primary" :disabled="selection.length == 0" @click="add" v-auth="'spare.add'">禁用</el-button>
					<el-button type="primary" :disabled="selection.length == 0" @click="add" v-auth="'spare.add'">启用</el-button>
					<sc-file-import :apiObj="$API.device.beiJian.goods.import" @success="success" ref="importBox" :templateUrl="'http://2063.mzth.cn/static/goods_import.xls'" v-if="showUp"></sc-file-import>
					<el-button type="primary" @click="add" v-auth="'spare.add'">导出</el-button>
					<el-button type="primary" :disabled="selection.length !== 1" @click="batch_edit" v-auth="'spare.edit'">修改</el-button>
					<el-button type="danger" :disabled="selection.length == 0" @click="batch_del" v-auth="'spare.deleteAll'">删除</el-button>

				</div>
			</el-header>
			<el-main class="nopadding">
				<scTable ref="table" row-key="id" :apiObj="apiObj" @selection-change="selectionChange" @row-dblclick="dblclick" stripe remoteSort remoteFilter border>
					<el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
                    <el-table-column type="index" label="序号" fixed align="center" width="50"></el-table-column>
					<el-table-column label="编号" prop="goods_code" align="center"></el-table-column>
					<el-table-column label="名称" prop="goods_name" align="center"></el-table-column>
					<el-table-column label="分组" prop="cate_name" align="center"></el-table-column>
					<el-table-column label="图片" align="center">
						<template #default="scope">
							<el-image
								v-if="scope.row.goods_image!=''"
								style="width: 30px; height: 30px; border-radius: 5px;"
								:src="scope.row.goods_image[0]"
								:preview-src-list="[scope.row.goods_image[0]]"
								preview-teleported="true"
								hide-on-click-modal ></el-image>
						</template>
					</el-table-column>
					<el-table-column label="规格型号" prop="goods_spec" align="center"></el-table-column>
                    <!-- <el-table-column label="购买方式" prop="pay_type" align="center">
                        <template #default="scope">
                            <el-tag v-if="scope.row.pay_type == 1" type="success">自制</el-tag>
                            <el-tag v-if="scope.row.pay_type == 2" type="primary">外购</el-tag>
                        </template>
                    </el-table-column> -->
					<el-table-column label="审核状态" prop="pay_type" align="center">
						<template #default="scope">
							<el-tag v-if="scope.row.pay_type == 1" type="success">自制</el-tag>
							<el-tag v-if="scope.row.pay_type == 2" type="primary">外购</el-tag>
						</template>
					</el-table-column>
					<el-table-column label="启用禁用" prop="pay_type" align="center">
						<template #default="scope">
							<el-tag v-if="scope.row.pay_type == 1" type="success">自制</el-tag>
							<el-tag v-if="scope.row.pay_type == 2" type="primary">外购</el-tag>
						</template>
					</el-table-column>
					<!-- <el-table-column label="批次" prop="goods_spec" align="center"></el-table-column>
					<el-table-column label="可销售" prop="is_sale" align="center">
						<template #default="scope">
							<el-tag v-if="scope.row.is_sale == 1" type="success">可销售</el-tag>
							<el-tag v-if="scope.row.is_sale == 0" type="danger">不可销售</el-tag>
						</template>
					</el-table-column>
					<el-table-column label="计量单位" prop="unit_name" align="center"></el-table-column>
					<el-table-column label="可采购" prop="is_pro" align="center">
                       <template #default="scope">
                           <el-tag v-if="scope.row.is_pro == 1" type="success">可采购</el-tag>
                           <el-tag v-if="scope.row.is_pro == 0" type="danger">不可采购</el-tag>
                       </template>
                   </el-table-column>
					<el-table-column label="操作" fixed="right" align="center" width="170">
						<template #default="scope">
							<el-button-group>
								<el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)" v-auth="'spare.show'">查看</el-button>
								<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)" v-auth="'spare.edit'">编辑</el-button>
								<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
									<template #reference>
										<el-button text type="danger" size="small" v-auth="'spare.delete'">删除</el-button>
									</template>
								</el-popconfirm>
							</el-button-group>
						</template>
					</el-table-column> -->
				</scTable>
			</el-main>
		</el-container>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false" draggable></save-dialog>
    <cate-save v-if="dialog.cate" ref="cateDialog" @success="typeHandleSuccess" @closed="dialog.cate = false" draggable></cate-save>
</template>

<script>
import CateSave from './cateSave.vue'
import saveDialog from './save.vue'

export default {
	name: '产品管理',
	components: {
		saveDialog,CateSave
	},
	data(){
		return {
			dialog: {
				save: false,
				cate: false,
			},
			apiObj: this.$API.device.beiJian.goods.lists,
			selection: [],
			search: {
				keywords: null,
				cate_id: null,
                unit_id:null,
				pay_type:null
			},
			menuList: [],
			menuProps: {
				label: (data) => {
					return data.cate_name
				}
			},
			menuloading: false,
			menuFilterText: "",
			menuForm: {},
            showUp:this.$TOOL.data.get("PERMISSIONS").includes('spare.import'),
            base:APP_CONFIG.API_URL,
            unitList:[],
		}
	},
	watch: {
		menuFilterText(val) {
			this.$refs.menu.filter(val);
		}
	},
	mounted() {
		this.getCate();
        this.getUnit();
	},
	methods: {
		/**双击事件*/
		dblclick(row) {
			this.table_show(row)
		},
		/**编辑点击*/
		batch_edit() {
			this.table_edit(this.selection[0])
		},
        success(res){
			if(res.code==1){
				this.$refs.importBox.close()
                this.$refs.table.refresh()
				this.$message.success("导入成功")
			} else{
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//加载树数据
		async getCate() {
			this.menuloading = true;
			var res = await this.$API.all.getBeiCate.get();
			this.menuloading = false;
			var allNode = { id: '', cate_name: '所有' }
			res.data.unshift(allNode);
			this.menuList = res.data;
            this.menuClick(allNode)
		},
        //树点击
		menuClick(data) {
			this.menuForm = data
			var params = {
				cate_id: data.id,
			}
			this.$refs.table.reload(params)
		},
		//树过滤
		menuFilterNode(value, data) {
			if (!value) return true;
			return data.cate_name.indexOf(value) !== -1;
		},
		/**删除菜单*/
		async delMenu() {
			if (!this.menuForm.id) {
				this.$message.warning("请选择需要删除的项")
				return false;
			}
			var confirm = await this.$confirm('确认删除已选择的分类吗？', '提示', {
				type: 'warning',
				confirmButtonText: '删除',
				confirmButtonClass: 'el-button--danger'
			}).catch(() => {
			})
			if (confirm != 'confirm') {
				return false
			}
			this.menuloading = true
			var reqData = {
				id: this.menuForm.id
			}
			var res = await this.$API.device.beiJian.cate.delete.post(reqData)
			this.menuloading = false
			if (res.code == 1) {
				this.getCate()
				this.menuForm = {}
			} else {
				this.$message.warning(res.message)
			}
		},
		/**添加分类*/
		addType() {
			this.dialog.cate = true
			this.$nextTick(() => {
				this.$refs.cateDialog.open('add')
			})
		},
		/**编辑分类*/
		editMenu() {
			if (!this.menuForm.id) {
				this.$message.warning("请选择需要编辑的项")
				return false;
			}
			this.dialog.cate = true
			this.$nextTick(() => {
				this.$refs.cateDialog.open('edit').setData(this.menuForm)
			})
		},
		/**分类添加成功事件*/
		typeHandleSuccess() {
			this.getCate();
		},
		//添加
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add')
			})
		},
        //添加
		up() {
			this.dialog.image = true
			this.$nextTick(() => {
				this.$refs.imageDialog.open('add')
			})
		},
		//编辑
		table_edit(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
		//查看
		table_show(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('show').setData(row)
			})
		},
		//删除
		async table_del(row, index) {
			var reqData = {id: row.id}
			var res = await this.$API.device.beiJian.goods.delete.post(reqData);
			if (res.code == 1) {
				//这里选择刷新整个表格 OR 插入/编辑现有表格数据
				this.$refs.table.tableData.splice(index, 1);
                this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async table_del_all(delete_ids) {
			var reqData = {id: delete_ids}
			var res = await this.$API.device.beiJian.goods.delete.post(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//批量删除
		async batch_del() {
			var delete_ids = [];
			this.selection.forEach((item) => {
				delete_ids.push(item.id)
			})
			this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(() => {
				this.table_del_all(delete_ids)
			}).catch(() => {

			})
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			}
		},
        handleKeyDown() {
            this.upsearch()
        },
        refresh(){
            for (const key in this.search) {
                this.search[key] = "";
            }
            this.upsearch()
        },
        async getUnit(){
            var res = await this.$API.all.getBeiUnit.get();
            this.unitList = res.data
        },
	}
}
</script>

<style></style>
